//r30
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.Timer;



public class r30_nphys extends JFrame implements MouseMotionListener{
	
	Rectangle r2 = new Rectangle(230, 190, 70, 100);
	
	
	
//	|_\
//	Point p1 = new Point(120,180);
//	Point p2 = new Point(120,300);
//	Point p3 = new Point(210,300);

	
//	|-/
//	Point p1 = new Point(120,180);
//	Point p2 = new Point(210, 180);
//	Point p3 = new Point(210,300);
	
//  /_|
	Point p1 = new Point(210,180);
	Point p2 = new Point(210,300);
	Point p3 = new Point(120,300);
	
	Point player = new Point(130,190);
	int p_radius = 20;
	
	
	public r30_nphys(){
		setTitle("N-PhysTutorial test");
		setSize(400,600);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Timer tmr = new Timer(10, new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				repaint();
			}
		});
		
		tmr.start();
		
		this.addMouseMotionListener(this);
	}
	
	public void paint(Graphics g){
		
		System.out.println("Player is at: "+player);
		
		
	
		
		Point n1vec = new Point((p3.y - p1.y),p1.x - p3.x); //RH-normal for vector P1P3
		double mag_n1 = n1vec.distance(0,0);
		
		double proj_a = (n1vec.x*(p2.x - p1.x) + n1vec.y*(p2.y-p1.y))/mag_n1;  //generalized projection for p2 = angle opposite hypot
				
		System.out.println("proja:"+proj_a);
		
		//Draw hypot-normal at P1...

		//draw the projection
		double theta = Math.atan2( n1vec.y, n1vec.x);
		
		
		
		Point p1cr = new Point( player.x - p1.x, player.y - p1.y );
		//Draw this vector...
	
		
		double cproj = project(p1cr, n1vec);
		System.out.println("Center projection of circle onto normal: "+cproj);
		
				
		
		double rr1 = cproj * Math.cos(theta)+p1.x;
		double rr2 = cproj * Math.sin(theta)+p1.y;
		//
		Graphics2D g2 = (Graphics2D)g;
		///////////////
		
		double r_ep = Math.max(Math.max(proj_a,0), cproj+p_radius);			
		double l_ep = Math.min(cproj - p_radius, Math.min(proj_a, 0));
		
		double overlap = -Math.abs(r_ep-l_ep) + Math.abs(proj_a ) + 2*p_radius;
		if(overlap > 0)
			g2.setBackground(Color.black);
		else		
			g2.setBackground(Color.white);
		g2.clearRect(0, 0, 1000, 1000);
		
		
		//Draw Triangle
		g.setColor(Color.red);
		g.drawLine(p1.x, p1.y, p2.x, p2.y);
		g.setColor(Color.blue);
		g.drawLine(p2.x, p2.y, p3.x, p3.y);
		g.setColor(Color.green);
		g.drawLine(p3.x, p3.y, p1.x, p1.y);
		
		
		g2.setColor(Color.lightGray);
		drawLine(g2,p1.x,p1.y,n1vec.x+p1.x,n1vec.y+p1.y);
		g2.setColor(Color.DARK_GRAY);
		drawLine(g2,p1.x,p1.y, proj_a * Math.cos(theta)+p1.x, proj_a* Math.sin(theta)+p1.y);
		
		g2.setColor(Color.MAGENTA);
		g2.drawOval(player.x-p_radius, player.y-p_radius, 2*p_radius, 2*p_radius);
		
		drawVectorAtPoint(g2, p1cr, p1);

		
		g2.setColor(Color.red);
		
		drawLine(g2,rr1 + p_radius*Math.cos(theta) ,rr2 + p_radius*Math.sin(theta) ,rr1 ,rr2);
		drawLine(g2,rr1 - p_radius*Math.cos(theta) ,rr2 - p_radius*Math.sin(theta) ,rr1 ,rr2);
		
//		g2.draw(r2);
//		
//		Point p1cr_vec = new Point((int)r2.getCenterX()-p1.x, (int)r2.getCenterY() - p1.y);
//		
//		double rect_reference = project(p1cr_vec,n1vec);
//		System.out.println("ref:"+rect_reference);
//		
//		//Project the p1-center vector onto N1 to find the center of the shape...
//		
//		double rect_halfshadow = Math.abs(project(new Point(0,r2.height/2),n1vec)) + Math.abs(project(new Point(r2.width/2,0),n1vec));
//		System.out.println("rec1/2shadow:"+rect_halfshadow);
//		
//		
//		//Draw some vectors to make shit pretty!
//		g2.setColor(Color.red);
//		double rr1 = rect_reference * Math.cos(theta)+p1.x;
//		double rr2 = rect_reference * Math.sin(theta)+p1.y;
//		drawLine(g2,rr1 + rect_halfshadow*Math.cos(theta) ,rr2 + rect_halfshadow*Math.sin(theta) ,rr1 ,rr2);
//		drawLine(g2,rr1 - rect_halfshadow*Math.cos(theta) ,rr2 - rect_halfshadow*Math.sin(theta) ,rr1 ,rr2);
//		
//		
//		//Collision testing:
//		
//		boolean colliding = false;
//		
//		//test overlap:
//		
//		if( proj_a <= rect_reference+rect_halfshadow && proj_a >= rect_reference - rect_halfshadow	)
//			System.out.println("touching along hypotenal axis");
//		else
//			System.out.println("not touching along hypotenal axis");
		
		
		
		
	}
	//Vector projection (v1 onto v2)
	public double project(Point v1,/*onto*/ Point v2){
		int a = ((v1.x*v2.x + v1.y*v2.y));
		return ((double)a)/v2.distance(0, 0);
	}
	
	public void drawLine(Graphics2D g2, double x, double y, double x1, double y1){
		g2.drawLine( (int)Math.round(x), (int)Math.round(y), (int)Math.round(x1), (int)Math.round(y1));
	}
	public void drawVectorAtPoint(Graphics2D g2, Point vector, Point at){
		drawLine(g2, at.x, at.y, vector.x + at.x, vector.y + at.y);
		
	}
	
	public static void main(String[]args){
		new r30_nphys();		
	}

	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		player = arg0.getPoint();
		System.out.println("MMved");
	}


}

/*
System.out.println("Player is at: "+player);

Graphics2D g2 = (Graphics2D)g;
g2.setBackground(Color.white);
g2.clearRect(0, 0, 1000, 1000);


//Draw Triangle
g.setColor(Color.red);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
g.setColor(Color.blue);
g.drawLine(p2.x, p2.y, p3.x, p3.y);
g.setColor(Color.green);
g.drawLine(p3.x, p3.y, p1.x, p1.y);



Point n1vec = new Point((p3.y - p1.y),p1.x - p3.x); //RH-normal for vector P1P3
double mag_n1 = n1vec.distance(0,0);

double proj_a = (n1vec.x*(p2.x - p1.x) + n1vec.y*(p2.y-p1.y))/mag_n1;  //generalized projection for p2 = angle opposite hypot
//double proj_a = n1vec.y*( p2.y - p1.y) / n1vec.distance(0,0); //only works for P2 below p1, to the left of p3
//double proj_a = project(new Point(p2.x-p1.x, p2.y-p1.y),n1vec ); //using the project method... except, making new objects is slow so screw this
		
System.out.println("proja:"+proj_a);

//Draw hypot-normal at P1...
g2.setColor(Color.lightGray);
drawLine(g2,p1.x,p1.y,n1vec.x+p1.x,n1vec.y+p1.y);
//draw the projection
double theta = Math.atan2( n1vec.y, n1vec.x);
g2.setColor(Color.DARK_GRAY);
drawLine(g2,p1.x,p1.y, proj_a * Math.cos(theta)+p1.x, proj_a* Math.sin(theta)+p1.y);

g2.setColor(Color.MAGENTA);
g2.drawOval(player.x-p_radius, player.y-p_radius, 2*p_radius, 2*p_radius);


Point p1cr = new Point( player.x - p1.x, player.y - p1.y );
//Draw this vector...
drawVectorAtPoint(g2, p1cr, p1);

double cproj = project(p1cr, n1vec);
System.out.println("Center projection of circle onto normal: "+cproj);


g2.setColor(Color.red);



double rr1 = cproj * Math.cos(theta)+p1.x;
double rr2 = cproj * Math.sin(theta)+p1.y;
drawLine(g2,rr1 + p_radius*Math.cos(theta) ,rr2 + p_radius*Math.sin(theta) ,rr1 ,rr2);
drawLine(g2,rr1 - p_radius*Math.cos(theta) ,rr2 - p_radius*Math.sin(theta) ,rr1 ,rr2);
*/